package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.laiyima.zhongjiang.linghuilv.demo.R;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class ElectroniContractActivity extends SwipeBackActivity implements View.OnClickListener {
    private LinearLayout mImageView;
    private Button myButton;
    private int ecCode;
    private String TAG="ElectroniContractActivity";
    private ImageView myImageView;

    private TextView textView;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
     };
    public static final String FILE_CONTENT_FILEPROVIDER = "com.laiyima.zhongjiang.linghuilv.demo.fileprovider";


    public static void verifyStoragePermissions(Activity activity) {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electroni_contract);
        mImageView= (LinearLayout) findViewById(R.id.ec_back);
        myButton= (Button) findViewById(R.id.agree_to_sign);
        myImageView= (ImageView) findViewById(R.id.show_sign_btimp);
        textView= (TextView) findViewById(R.id.show_sign_time);
        mImageView.setOnClickListener(this);
        myButton.setOnClickListener(this);
        intView();
        setStatusBar();
        verifyStoragePermissions(this);
       // showIcon();

    }
    @Override
    protected void onResume() {
        super.onResume();
        showIcon();
    }

    private void showIcon() {
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String icon_path = sharedPreferences.getString("sign_image", "");
        String Sig_date = sharedPreferences.getString("Sig_date", "");
        textView.setText("  日期："+Sig_date);
        Log.e(TAG, "sign_image        " + icon_path);
        if (icon_path != null &&isFileExist(icon_path)) {
            Bitmap bitmap = BitmapFactory.decodeFile(icon_path);
            Log.e(TAG, "sign_image        " + bitmap);
            myImageView.setImageBitmap(bitmap);
        }

    }



    public static boolean isFileExist(String icon_path) {
        File file = new File(icon_path);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    private void intView() {
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/hjs_api/Real/query_sign");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {


                try {
                    JSONObject jsonObject = new JSONObject(result);
                    ecCode = jsonObject.optInt("code", 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.e(TAG, "回调成功"+ecCode);
                if (ecCode == 200) {
                    SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE); //私有数据
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("Signing", "false");
                    editor.commit();
                   // Toast.makeText(getBaseContext().getApplicationContext(), "你已签约", Toast.LENGTH_SHORT).show();
                }
                if (ecCode == 404) {
                    //Toast.makeText(getBaseContext().getApplicationContext(), "你还未签约", Toast.LENGTH_SHORT).show();
                    SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE); //私有数据
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("Signing", "true");
                    editor.commit();
                }
                if (ecCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                   // Log.e(TAG, "系统异常");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
               // Log.e(TAG, "查看实名认证失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
                String yijinqianyue = sharedPreferences.getString("Signing", "");
                Log.e(TAG, "yijinqianyue"+yijinqianyue);
                if (yijinqianyue.equals("false")){
                    myButton.setText("你已签约");
                   // myButton.setEnabled(false);
                    myButton.setBackgroundResource(R.drawable.sginbutton);
                    myButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                }

            }
        });
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.ec_back:
                finish();
                break;
            case R.id.agree_to_sign:
                Intent userintent=new Intent(this,SugnatureActivity.class);
                startActivity(userintent);
                break;
        }
    }
}
